package com.sunny.shop.service;

import com.github.dozermapper.core.DozerBeanMapperBuilder;
import com.github.dozermapper.core.Mapper;
import com.sunny.shop.core.LocalUser;
import com.sunny.shop.model.GoodsOrder;
import com.sunny.shop.repository.GoodsOrderRepository;
import com.sunny.shop.util.CommonUtil;
import com.sunny.shop.vo.GoodsOrderVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class GoodsOrderServiceImpl {
    @Autowired
    private GoodsOrderRepository goodsOrderRepository;

    public List<GoodsOrderVO> getOrderList(){
        Long uid = LocalUser.getUser().getId();
        Mapper mapper = DozerBeanMapperBuilder.buildDefault();
        List<GoodsOrder> all = this.goodsOrderRepository.findAllByUid(uid);
        List<GoodsOrderVO> list = new ArrayList<>();
        all.forEach(item->{
            GoodsOrderVO map = mapper.map(item, GoodsOrderVO.class);
            map.setCreateTime(CommonUtil.getSimpleData(item.getCreateTime()));
            list.add(map);
        });
        return list;
    }

    public GoodsOrder getOrderDetail(String order_sn){
        Long uid = LocalUser.getUser().getId();
        GoodsOrder orderDetail = this.goodsOrderRepository.findGoodsOrderByOrderSnAndUid(order_sn,uid);
        return orderDetail;
    }
}
